package com.hand.demo.app.service.impl;

import io.choerodon.core.domain.Page;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import com.hand.demo.app.service.HhrTaCertService;
import org.springframework.stereotype.Service;
import com.hand.demo.domain.entity.HhrTaCert;
import com.hand.demo.domain.repository.HhrTaCertRepository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 证书表(HhrTaCert)应用服务
 *
 * @author
 * @since 2025-06-16 12:16:54
 */
@Service
public class HhrTaCertServiceImpl implements HhrTaCertService {
    @Autowired
    private HhrTaCertRepository hhrTaCertRepository;

    @Override
    public Page<HhrTaCert> selectList(PageRequest pageRequest, HhrTaCert hhrTaCert) {
        return PageHelper.doPageAndSort(pageRequest, () -> hhrTaCertRepository.selectList(hhrTaCert));
    }

    @Override
    public void saveData(List<HhrTaCert> hhrTaCerts) {
        List<HhrTaCert> insertList = hhrTaCerts.stream().filter(line -> line.getId() == null).collect(Collectors.toList());
        List<HhrTaCert> updateList = hhrTaCerts.stream().filter(line -> line.getId() != null).collect(Collectors.toList());
        hhrTaCertRepository.batchInsertSelective(insertList);
        hhrTaCertRepository.batchUpdateByPrimaryKeySelective(updateList);
    }

    @Override
    public void deleteByResumeId(Long resumeId) {
        // 创建查询条件
        HhrTaCert query = new HhrTaCert();
        query.setResumeId(resumeId);
        // 查询该简历下的所有证书记录
        List<HhrTaCert> existingList = hhrTaCertRepository.selectList(query);
        // 如果存在数据 则进行删除操作
        if (!existingList.isEmpty()) {
            hhrTaCertRepository.batchDeleteByPrimaryKey(existingList);
        }

    }
}

